package basic.chapter6;

import java.util.List;

/**
 * @author Kevin
 * @date 2020/2/21 20:56
 */
public class Allocator {
    private List<Object> als;
    //一次性申请所有资源
    synchronized void apply(Object from, Object to) {
        //经典写法
        while (als.contains(from) || als.contains(to)) {//条件不满足，就是有人被占用
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        als.add(from);
        als.add(to);
    }

    synchronized void free(Object from, Object to) {
        als.remove(from);
        als.remove(to);
        notifyAll();
    }
}
